//<![CDATA[        
$(document).ready(function() {		
	//Report bug to sys admin
	$('.bug_report').click(function(){
		if($('.bug_subj').text() != ""){
			url = '/admin/reportbug/'
			window.location.href = url	
		}			
	});	
		
	//Store tmz value
	tmz = $('#id_tmz').text();
	
	//Populate tmz select box; Too many values to put into python : )
	$('#id_tmz').replaceWith('<select name=tmz>' + isDayLight() + '</select>');
	
	//Autoselect carrier and timezone	     
	 selectCarrier();
	 selectTimezone(tmz);
	
	//Initialize Edit Profile Form
	 
	/*Setup Edit Form for Validation
	$($('#editProfile th').get(1)).text("Birthday: ");
	$($('#editProfile th').get(2)).text("Phone Carrier: ");
	$($('#editProfile th').get(3)).text("Phone Number: ");
	$($('#editProfile th').get(4)).text("Due Date: ");
	$($('#editProfile th').get(5)).text("Pre-Pregnancy Weight: ");
	$($('#editProfile th').get(8)).text("BMI: ");
	$($('#editProfile th').get(9)).text("Timezone: ");*/
	
	$('#id_name').addClass("required").attr({minlength: "2"});
	$('#id_bday').mask("99/99/9999").addClass("required date");
	$('#id_phone_num').mask("(999) 999-9999").addClass("required phoneUS");
	$('#id_due_date').mask("99/99/9999").addClass("required date");	
	$('#id_pre_weight').addClass("required").attr({maxlength: "3"});
	$('#id_height').addClass("required").attr({maxlength: "2"}).after("<label> in.</label");;	
	$('#id_age').addClass("required").attr({minlength: "2"}).attr({maxlength: "2"});
	$('#id_bmi').addClass("required");
	$('#id_height,#id_pre_weight').bind("keyup blur", function(){ calcBMI() });
	$('#id_bmi').click(function(){ calcBMI() });
	
	//Validate Edit Form
	$('#editForm').validate();
	
	//Enable message preview
	$('.msg_id').hover({		
		//Show corresponding message in tooltip    
	});
}); 

/***************************************************************************************
 *                              HELPER METHODS
 ***************************************************************************************/

/**
 * Prepopulate Phone Carrier Dropdown
 */
function selectCarrier(){
	//Preselects user's carrier
	var carriers = [];
	carriers[0] = "AT&T";
	carriers[1] = "Alltel";
	carriers[2] = "T-mobile";
	carriers[3] = "Verizon";
	carriers[4] = "Virign Mobile";
	carriers[5] = "Suncom";
	carriers[6] = "Sprint (PCS)";
	carriers[7] = "Sprint (Nextel)";
	carriers[8] = "Cingular GoPhone";

	carrier_now = $('#phone_car').val();
	var carrierList = document.getElementById("carrier");
	carrier_index = carriers.indexOf(carrier_now)
	var curOption = document.getElementById( carriers[carrier_index] );
	if (curOption != null) {
		curOption.selected = true;
	}
}

/**
 * Autopopulate timezone dropdown
 * @param {Object} tmz
 */
function selectTimezone(tmz){
	//Preselects user's timezone
	var timezones = [];
	timezones[0] = "-12.0";
	timezones[1] = "-11.0";
	timezones[2] = "-10.0";
	timezones[3] = "-9.0";
	timezones[4] = "-8.0";
	timezones[5] = "-7.0";
	timezones[6] = "-6.0";
	timezones[7] = "-5.0";
	timezones[8] = "-4.0";
	timezones[9] = "-3.5";
	timezones[10] = "-3.0";
	timezones[11] = "-2.0";
	timezones[12] = "-1.0";
	timezones[13] = "0.0";
	timezones[14] = "1.0";
	timezones[15] = "2.0";
	timezones[16] = "3.0";
	timezones[17] = "3.5";
	timezones[18] = "4.0";
	timezones[19] = "4.5";
	timezones[20] = "5.0";
	timezones[21] = "5.5";
	timezones[22] = "5.75";
	timezones[23] = "6.0";
	timezones[24] = "7.0";
	timezones[25] = "8.0";
	timezones[26] = "9.0";
	timezones[27] = "9.5";
	timezones[28] = "10.0";
	timezones[29] = "11.0";
	timezones[30] = "12.0";
	
	var tmzList = document.getElementById("id_tmz");
	tmz_index = timezones.indexOf(tmz);
	var curOption = document.getElementById( timezones[tmz_index] );
	if (curOption != null) {	curOption.selected = true;	}	
}

/**
 * Calculate if Day Light Savings is in effect
 */
function isDayLight() {
 	//Determines if it is daylight savings time
    // 2nd sunday of march
    oDate = new Date();
    var dstStartDate = new Date();
    dstStartDate.setMonth(2);
    dstStartDate.setDate(1);
    dstStartDate.setDate(8 - dstStartDate.getDay() + 7);
 
    // first sunday of november
    var dstEndDate = new Date();
    dstEndDate.setMonth(10);
    dstEndDate.setDate(1);
    dstEndDate.setDate(8 - dstEndDate.getDay());
 
    if ((oDate.getMonth() > dstStartDate.getMonth()) && (oDate.getMonth() < dstEndDate.getMonth())) {
        bInDST = true;
    } else if (oDate.getMonth() == dstStartDate.getMonth()) {
        if (oDate.getDate() >= dstStartDate.getDate()) {
            bInDST = true;
        } else {
            bInDST = false;
        }
    } else if (oDate.getMonth() == dstEndDate.getMonth()) {
        if (oDate.getDate() < dstEndDate.getDate()) {
            bInDST = true;
        } else {
            bInDST = false;
        }
    } else {
        bInDST = false;
    }
	
	if(bInDST == false){
		content = "<option id=-10.0 value=-10.0>(GMT -10:00) Hawaii</option>" + 
			      "<option id=-9.0 value=-9.0>(GMT -9:00) Alaska</option>" + 
			      "<option id=-8.0 value=-8.0>(GMT -8:00) Pacific Time (US &amp; Canada)</option>" + 
			      "<option id=-7.0 value=-7.0>(GMT -7:00) Mountain Time (US &amp; Canada)</option>" + 
			      "<option id=-6.0 value=-6.0>(GMT -6:00) Central Time (US &amp; Canada), Mexico City</option>" + 
			      "<option id=-5.0 value=-5.0 selected=selected>(GMT -5:00) Eastern Time (US &amp; Canada), Bogota, Lima</option>" + 
			      "<option id=-4.0 value=-4.0>(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz</option>" + 
			      "<option id=-3.5 value=-3.5>(GMT -3:30) Newfoundland</option>" + 
			      "<option id=-3.0 value=-3.0>(GMT -3:00) Brazil, Buenos Aires, Georgetown</option>" + 
			      "<option id=-2.0 value=-2.0>(GMT -2:00) Mid-Atlantic</option>" + 
			      "<option id=-1.0 value=-1.0>(GMT -1:00 hour) Azores, Cape Verde Islands</option>" + 
			      "<option id=0.0 value=0.0>(GMT) Western Europe Time, London, Lisbon, Casablanca</option>" + 
			      "<option id=1.0 value=1.0>(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris</option>" + 
			      "<option id=2.0 value=2.0>(GMT +2:00) Kaliningrad, South Africa</option>" + 
			      "<option id=3.0 value=3.0>(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg</option>" + 
			      "<option id=3.5 value=3.5>(GMT +3:30) Tehran</option>" + 
			      "<option id=4.0 value=4.0>(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi</option>" + 
			      "<option id=4.5 value=4.5>(GMT +4:30) Kabul</option>" + 
			      "<option id=5.0 value=5.0>(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent</option>" + 
			      "<option id=5.5 value=5.5>(GMT +5:30) Bombay, Calcutta, Madras, New Delhi</option>" + 
			      "<option id=5.75 value=5.75>(GMT +5:45) Kathmandu</option>" + 
			      "<option id=6.0 value=6.0>(GMT +6:00) Almaty, Dhaka, Colombo</option>" + 
			      "<option id=7.0 value=7.0>(GMT +7:00) Bangkok, Hanoi, Jakarta</option>" + 
			      "<option id=8.0 value=8.0>(GMT +8:00) Beijing, Perth, Singapore, Hong Kong</option>" + 
			      "<option id=9.0 value=9.0>(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk</option>" + 
			      "<option id=9.5 value=9.5>(GMT +9:30) Adelaide, Darwin</option>" + 
			      "<option id=10.0 value=10.0>(GMT +10:00) Eastern Australia, Guam, Vladivostok</option>" + 
			      "<option id=11.0 value=11.0>(GMT +11:00) Magadan, Solomon Islands, New Caledonia</option>" + 
			      "<option id=12.0 value=12.0>(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka</option>"		
	}else{
		content = "<option id=-10.0 value=-10.0>(GMT -10:00) Hawaii</option>" + 
			      "<option id=-9.0 value=-9.0>(GMT -8:00) Alaska</option>" + 
			      "<option id=-8.0 value=-8.0>(GMT -7:00) Pacific Time (US &amp; Canada)</option>" + 
			      "<option id=-7.0 value=-7.0>(GMT -6:00) Mountain Time (US &amp; Canada)</option>" + 
			      "<option id=-6.0 value=-6.0>(GMT -5:00) Central Time (US &amp; Canada), Mexico City</option>" + 
			      "<option id=-5.0 value=-5.0 selected=selected>(GMT -4:00) Eastern Time (US &amp; Canada), Bogota, Lima</option>" + 
			      "<option id=-4.0 value=-4.0>(GMT -3:00) Atlantic Time (Canada), Caracas, La Paz</option>" + 
			      "<option id=-3.5 value=-3.5>(GMT -3:30) Newfoundland</option>" + 
			      "<option id=-3.0 value=-3.0>(GMT -3:00) Brazil, Buenos Aires, Georgetown</option>" + 
			      "<option id=-2.0 value=-2.0>(GMT -2:00) Mid-Atlantic</option>" + 
			      "<option id=-1.0 value=-1.0>(GMT -1:00 hour) Azores, Cape Verde Islands</option>" + 
			      "<option id=0.0 value=0.0>(GMT) Western Europe Time, London, Lisbon, Casablanca</option>" + 
			      "<option id=1.0 value=1.0>(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris</option>" + 
			      "<option id=2.0 value=2.0>(GMT +2:00) Kaliningrad, South Africa</option>" + 
			      "<option id=3.0 value=3.0>(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg</option>" + 
			      "<option id=3.5 value=3.5>(GMT +3:30) Tehran</option>" + 
			      "<option id=4.0 value=4.0>(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi</option>" + 
			      "<option id=4.5 value=4.5>(GMT +4:30) Kabul</option>" + 
			      "<option id=5.0 value=5.0>(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent</option>" + 
			      "<option id=5.5 value=5.5>(GMT +5:30) Bombay, Calcutta, Madras, New Delhi</option>" + 
			      "<option id=5.75 value=5.75>(GMT +5:45) Kathmandu</option>" + 
			      "<option id=6.0 value=6.0>(GMT +6:00) Almaty, Dhaka, Colombo</option>" + 
			      "<option id=7.0 value=7.0>(GMT +7:00) Bangkok, Hanoi, Jakarta</option>" + 
			      "<option id=8.0 value=8.0>(GMT +8:00) Beijing, Perth, Singapore, Hong Kong</option>" + 
			      "<option id=9.0 value=9.0>(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk</option>" + 
			      "<option id=9.5 value=9.5>(GMT +9:30) Adelaide, Darwin</option>" + 
			      "<option id=10.0 value=10.0>(GMT +10:00) Eastern Australia, Guam, Vladivostok</option>" + 
			      "<option id=11.0 value=11.0>(GMT +11:00) Magadan, Solomon Islands, New Caledonia</option>" + 
			      "<option id=12.0 value=12.0>(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka</option>"			
	}
    return content;
}

/**
 * Calculate BMI using height and wright
 */
function calcBMI(){
	height = $('#id_height').val();
	weight = $('#id_pre_weight').val();
	if (height != null && weight != null) {
		bmi = (weight * 703) / height;
		bmi = Math.round(bmi * 100) / 100;
		$('#id_bmi').val(bmi);
	}
}	

function getInput(t){
	if(t != ""){
		if(validate_email(t) == true){			
			url = '/profile/' + t;
			window.location.href = url
		}
	}
}

function getMsgID(id){
	if(id == "WE1") return "First message sent to all users as a greeting."	
}

this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("a.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};
//]]>
